﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.ComponentModel;

public static class UtilsModelCopier
{

    /// <summary>
    /// AutoMapper 组件进行映射
    /// </summary>
    /// <typeparam name="TDestination"></typeparam>
    /// <typeparam name="TSource"></typeparam>
    /// <param name="source"></param>
    /// <returns></returns>
    public static TDestination MapModel<TDestination, TSource>(TSource source)
    {
        var config = new MapperConfiguration(cfg => {
            cfg.CreateMap<TSource, TDestination>();
        });
        IMapper iMapper = config.CreateMapper();
        var destination = iMapper.Map<TDestination>(source);
        return destination;
    }


    public static void CopyCollection<T>(IEnumerable<T> from, ICollection<T> to)
    {
        if (from == null || to == null || to.IsReadOnly)
        {
            return;
        }

        to.Clear();
        foreach (T element in from)
        {
            to.Add(element);
        }
    }

    public static void CopyModel(object from, object to)
    {
        if (from == null || to == null)
        {
            return;
        }

        PropertyDescriptorCollection fromProperties = TypeDescriptor.GetProperties(from);
        PropertyDescriptorCollection toProperties = TypeDescriptor.GetProperties(to);

        foreach (PropertyDescriptor fromProperty in fromProperties)
        {
            PropertyDescriptor toProperty = toProperties.Find(fromProperty.Name, true /* ignoreCase */);
            if (toProperty != null && !toProperty.IsReadOnly)
            {
                // Can from.Property reference just be assigned directly to to.Property reference?
                bool isDirectlyAssignable = toProperty.PropertyType.IsAssignableFrom(fromProperty.PropertyType);
                // Is from.Property just the nullable form of to.Property?
                bool liftedValueType = (isDirectlyAssignable) ? false : (Nullable.GetUnderlyingType(fromProperty.PropertyType) == toProperty.PropertyType);

                if (isDirectlyAssignable || liftedValueType)
                {
                    object fromValue = fromProperty.GetValue(from);
                    if (isDirectlyAssignable || (fromValue != null && liftedValueType))
                    {
                        toProperty.SetValue(to, fromValue);
                    }
                }
            }
        }
    }

}